package com.lz.gmall.user.controller;

import com.lz.gmall.common.result.Result;
import com.lz.gmall.user.dto.UserLoginDto;
import com.lz.gmall.user.service.UserAddressService;
import com.lz.gmall.user.service.UserInfoService;
import com.lz.gmall.user.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user/passport")
public class UserLoginController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 单点登录，判断用户的登录信息在数据库中是否存在
     * 在登录页面，点击登录发送请求到达这里
     * @param userLoginDto 用户登录信息
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto){
        UserLoginVo userLoginVo = userInfoService.checkUserLogin(userLoginDto);
        return Result.ok(userLoginVo);
    }

//    @GetMapping("/logout")
//    public Result logout(HttpServletRequest request){
//        userInfoService.logout(request);
//        return Result.ok();
//    }

    /**
     * 退出登录状态，删除redis中的数据
     * @return
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader(value = "token") String token){
        userInfoService.logout(token);
        return Result.ok();
    }
}
